/*
 * The use this code commercially must only be done with permission of the author.
 * Any modification shall be advised and sent to the author.
 * The author is not responsible for any problem therefrom the use of this frameWork.
 *
 * @author Gefersom Cardoso Lima
 * Federal Fluminense University
 * Computer Science
 */

package Jplay;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * Class responsible for handling mouse actions.
 */
public class Mouse extends InputBase implements MouseMotionListener, MouseListener {

    private Point mousePosition;

    private boolean leftButtonPressionado;
    private boolean rightButtonPressionado;
    private boolean leftButtonClicou;
    private boolean rightButtonClicou;

    public static final int BUTTON_LEFT = 1;
    public static final int BUTTON_MIDDLE = 2;
    public static final int BUTTON_RIGHT = 3;

    /**
     * Class constructor.
     * Creates an instance of the mouse positioned at (x=0, y=0).
     * The behavior of the buttons (right, middle, left) is DECTECT_INITIAL_PRESS_ONLY.
     */
    public Mouse()
    {
            mousePosition = new Point(0, 0);
            leftButtonPressionado = false;
            rightButtonPressionado = false;
            leftButtonClicou = false;
            rightButtonClicou = false;
    }

    /**
     *Returns the position of the mouse on the screen.
     *@return Point
     */
    public Point getPosition()
    {
            return mousePosition;
    }

    /**
     * Returns true if the left button is pressed, otherwise false.
     *@return boolean
     */
    public boolean isLeftButtonPressed()
    {
            boolean retorno = false;
            if (leftButtonPressionado == true)
            {
                leftButtonClicou = true;
            }
            if (leftButtonPressionado == false && leftButtonClicou == true)
            {
                leftButtonClicou = false;
                retorno = true;
            }
            return retorno;
    }



    /**
     * Returns true if the right button is pressed, otherwise false.
     *@return boolean
     */
    public boolean isRightButtonPressed()
    {
            boolean retorno = false;
            if (rightButtonPressionado == true)
            {
                rightButtonClicou = true;
            }
            if (rightButtonPressionado == false && rightButtonClicou == true)
            {
                rightButtonClicou = false;
                retorno = true;
            }
            return retorno;
    }


    public void mouseClicked(MouseEvent e)
    {
       
    }


    public void mousePressed(MouseEvent e)
    {
        switch (e.getButton())
        {
            case MouseEvent.BUTTON1: leftButtonPressionado = true;   break;
            case MouseEvent.BUTTON3: rightButtonPressionado = true;  break;
        }
    }

    public void mouseReleased(MouseEvent e)
    {
            switch (e.getButton())
            {
            case MouseEvent.BUTTON1: leftButtonPressionado = false;   break;
            case MouseEvent.BUTTON3: rightButtonPressionado = false;  break;
            }
    }
    
    public void mouseRelease()
    {

    }

    public void mouseEntered(MouseEvent e) {
    	mousePosition = e.getPoint();
    }

    public void mouseExited(MouseEvent e) {
    	mousePosition = e.getPoint();
    }

    public void mouseDragged(MouseEvent e)
    {
            mousePosition = e.getPoint();
    }

    public void mouseMoved(MouseEvent e)
    {
            mousePosition = e.getPoint();
    }

    /**
     * Returns true if the mouse is over an object, otherwise, return false.
     * @param obj any GameObject.
     * @return boolean
     */
    
    public boolean isOverObject(GameObject obj)
    {
            Point min = new Point( (int)obj.x, (int)obj.y);
            Point max = new Point( (int)(obj.x + obj.width), (int)(obj.y + obj.height));
            return isOverArea(min, max);
    }

    /**
     * Returns true if the mouse is over an object, otherwise, return false.
     * @param start initial point of the area.
     * @param end final point of the area.
     * @return boolean
     */
    public boolean isOverArea( Point start, Point end )
    {
            return  isOverArea(start.x, start.y, end.x, end.y);
    }

    /**
     * Returns true if the mouse is over an area, otherwise, return false
     * @minX minus value of the axes X.
     * @minY minus value of the axes Y.
     * @maxX minus value of the axes X.
     * @maxX minus value of the axes Y.
     * @return boolean
     */
    public boolean isOverArea( double x, double y, double d, double e )
    {
            if ((mousePosition.x < x) || (mousePosition.x > d))
                return false;

            if ((mousePosition.y < y) || (mousePosition.y > e))
                return false;

            return true;
    }

}
